import static org.junit.Assert.fail;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.junit.Test;


public class SemanticAnalysisTests {
	private FileInputStream openFile(String filename) {
		try {
			FileInputStream fileContents = new FileInputStream(filename);
			return fileContents;
		}
		catch (FileNotFoundException e) {
			fail("Ficheiro " + filename + " nao existente");
			return null;
		}
	}

	private SimpleNode getRootNodeOfFile(String filename) {
		eg1 parser = new eg1(openFile(filename));

		SimpleNode runOutput = null;
		try {
			runOutput = parser.Start();
			//runOutput.dump("");
		}
		catch (ParseException e) {
			fail();
		}
		return runOutput;
	}
	
	@Test
	public void testErr1() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err1.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarFuncoesComScalarReturnFazemAssign();
		}
		catch (WrongFunctionReturnException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr2() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err2.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarFuncoesSemReturnoNaoFazemAssign();
		}
		catch (WrongFunctionReturnException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr3() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err3.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarFuncoesSemReturnoNaoFazemAssign();
		}
		catch (WrongFunctionReturnException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr4() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err4.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarSeFuncaoExisteEmModulo();
		}
		catch (FunctionNotFoundInModuleException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr5() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err5.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarArgumentosDasFuncoes();
		}
		catch (WrongFunctionPrototypeException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr6() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err6.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarArgumentosDasFuncoes();
		}
		catch (WrongFunctionPrototypeException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr7() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err7.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarNumeroDeArgumentosEmFuncoes();
		}
		catch (WrongFunctionPrototypeException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr8() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err8.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarTipoDeArgumentosEmFuncoes();
		}
		catch (WrongFunctionPrototypeException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr9() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err9.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarTipoDeArgumentosEmFuncoes();
		}
		catch (WrongFunctionPrototypeException e) {
			return;
		}
		
		fail();
	}

	@Test
	public void testErr10() {
		SimpleNode rootNode = getRootNodeOfFile("Eval-Set3-YAL/err10.yal");
		
		SemanticAnalysis analiseSemantica = new SemanticAnalysis(rootNode);
		
		try {
			analiseSemantica.verificarTipoDeRetornoDasFuncoes();
		}
		catch (WrongFunctionReturnException e) {
			return;
		}
		
		fail();
	}
}
